#! /bin/bash
#
# install.sh
# Copyright (C) 2020 undefined <undefined>
#
# Distributed under terms of the MIT license.
#
user=gbasedbt
group=gbasedbt


command_exists() {
	command -v "$@" >/dev/null 2>&1
}
#command_exists java||{
#    echo "未安装JDK，程序退出"
#    exit 1
#
#}
##create group if not exists
#egrep "^$group" /etc/group >& /dev/null
#if [ $? -ne 0  ]
#then
#        groupadd $group
#fi
#
##create user if not exists
#egrep "^$user" /etc/passwd >& /dev/null
#if [ $? -ne 0  ]
#then
#        useradd -g $group -m -s /bin/bash $user
#fi
# if [ -d /home/gbasedbt/server ]
# then 
#     rm -rf /home/gbasedbt/server/*
# else
#     mkdir /home/gbasedbt/server
#     chown -R gbasedbt:gbasedbt /home/gbasedbt/server
#     chmod -R 644 /home/gbasedbt/server
# fi
#
# if [ -d /etc/gbasedbt ]
# then
#     echo "USERS:daemon">/etc/gbasedbt/allowed.surrogates
# else
#     mkdir /etc/gbasedbt
#     echo "USERS:daemon">/etc/gbasedbt/allowed.surrogates
# fi
#
#chmod +x ./Gbase/ids_install
#( echo '\n'
#sleep 1
#echo '\n'
#sleep 1
#echo '\n'
#sleep 1
#echo '\n'
#sleep 1
#echo '\n'
#sleep 1
#echo "y"
#sleep 1
#echo "/home/gbasedbt/server"
#sleep 3
#echo "Y"
#sleep 1
#echo "2"
#sleep 1
#echo '\n'
#sleep 1
#echo "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24"
#sleep 1
#echo '1'
#sleep 1
#echo '2'
#sleep 1
#echo '2'
#sleep 1
#echo '\n'
#sleep 1
#echo '\n'
#sleep 1
#echo '\n'
#
#
#)|./Gbase/ids_install
#
command_exists expect||{
    echo "Expect not found!"
    sudo apt install expect
    if [ $? -eq 0  ]
    then
    	exit 1
    fi
}
#/usr/bin/expect << EOF
#set timeout 5
#spawn ./Gbase/ids_install
#expect {
#	"CONTINUE" { send "\r"; exp_continue  }
#        "AGREEMENT? (Y/N):" { send "Y\r"; exp_continue  }
#        "*yes/no*" { send "yes\r";exp_continue }
#        "password:" { send "$PASS\n";exp_continue } 
#        }
#EOF


cp ./GBaseInit_gbasedbt.sh /home/gbasedbt/server
chown gbasedbt:gbasedbt /home/gbasedbt/server/GBaseInit_gbasedbt.sh
su - gbasedbt <<EOF
chmod +x /home/gbasedbt/server/GBaseInit_gbasedbt.sh

/usr/bin/expect
set timeout 5
spawn ~/server/GBaseInit_gbasedbt.sh
expect {
        "AGREEMENT? (Y/N):" { send "Y\r"; exp_continue  }
        "*yes/no*" { send "yes\r";exp_continue }
	"*Default:gbaseserver*" { send "\r";exp_continue  }
	"CHOOSE GBASE INSTALL FOLDER*" { send "/home/gbasedbt/server\r";exp_continue  }
	"CHOOSE SERVICE IP ADDRESS FROM THE LIST*ENTER THE NUMBER FOR YOUR CHOICE*" { send "\n";exp_continue  }
	"SPECIFY THE PORT NUMBER FOR GBASE*" { send "9088\r"; exp_continue }
	"INITIALIZE TYPE*ENTER THE NUMBER FOR YOUR CHOICE" { send "2\r"; exp_continue  }
	"GBASE CHARACTER SET LIST*ENTER THE NUMBER FOR YOUR CHOICE" { send "3\r"; exp_continue  }
	"ENTER THE DATA SPACE PATH*" { send "\r"; exp_continue }	
	"ENTER THE LOGICAL LOG DBSPACE SIZE*" { send "100\r"; exp_continue  }
	"ENTER THE PHYSICAL LOG DBSPACE SIZE*" { send "100\r"; exp_continue  }
	"ENTER THE SMART LOB DBSPACE SIZE*" { send "100\r"; exp_continue  }
	"ENTER THE TEMPORARY DBSPACE SIZE(MB)*" { send "100\r"; exp_continue  }
	"ENTER \"Y\" TO START DATABASE ADVANCED SETTINGS*" { send "N\r"; exp_continue }	
	"Enter \"Y\" to Start database initializing*" { send "Y\r"; exp_continue  }
	eof{ exit  }
        }

EOF
